/*
 * @Author: '浪川' '1214391613@qq.com'
 * @Date: 2025-08-08 10:00:17
 * @LastEditors: '浪川' '1214391613@qq.com'
 * @LastEditTime: 2025-08-08 17:45:33
 * @FilePath: \passiflora-edulis-sims\src\stores\global-option-store.ts
 * @Description:全局选项设置，例如字体
 *
 * Copyright (c) 2025 by '1214391613@qq.com', All Rights Reserved.
 */

import { defineStore, acceptHMRUpdate } from 'pinia'

export const useGlobalOptionStore = defineStore('GlobalOption', {
  state: () => ({
    font_infos: {} as Record<string, string>,
    text_aligns: ['center', 'right', 'left', 'justify', 'both'],
    vertical_aligns: ['middle', 'top', 'bottom'],
  }),

  getters: {
    font_names(state): string[] {
      const lst: string[] = []
      for (const key in state.font_infos) {
        lst.push(key)
      }
      return lst
    },
  },

  actions: {
    readFontList() {
      const fontFiles = Object.entries(
        import.meta.glob('/public/fonts/*.{ttf,otf,woff,woff2,eot}', {
          eager: true,
          as: 'url',
        })
      ).map(([path, url]) => [
        path.split('/').pop() || '', // 文件名作为键
        url, // 直接使用url，不需要.default
      ])
      const dic: Record<string, string> = {}
      for (const k of fontFiles) {
        let key = k[0] as string
        const val = k[1] as string
        key = key.split('.')[0] as string
        dic[key] = val
      }
      this.font_infos = dic
      console.log(fontFiles)
    },
    get_fontPath(font_name: string): string {
      if (this.font_infos[font_name]) {
        return this.font_infos[font_name]
      } else {
        return ''
      }
    },
  },
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useGlobalOptionStore, import.meta.hot))
}
